package com.wiki.gds.user.provider.modular.sys.controller;

import com.wiki.gds.base.common.dto.ResponseDto;
import com.wiki.gds.base.common.exception.BizExceptionEnum;
import com.wiki.gds.base.security.admin.utils.SecurityUtil;
import com.wiki.gds.base.security.common.model.SecurityUser;
import com.wiki.gds.base.web.controller.common.BaseController;
import com.wiki.gds.user.facade.sys.model.Resource;
import com.wiki.gds.user.facade.sys.model.Role;
import com.wiki.gds.user.facade.sys.model.query.ResourceQuery;
import com.wiki.gds.user.facade.sys.vo.vue.MenuVo;
import com.wiki.gds.user.provider.modular.sys.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("resource")
@Api(value = "ResourceController",tags = "资源控制器")
@Validated
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("menu")
    @ApiOperation(value = "获取菜单",notes = "获取菜单",httpMethod = "POST")
    @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header")
    public ResponseDto menu(){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        SecurityUser securityUser= SecurityUtil.getSecurityUser();
        List<MenuVo> menuVoList= resourceService.getMenu(securityUser.getId());
        rs.setData(menuVoList);
        return rs;
    }

    @PostMapping("list")
    @ApiOperation(value = "获取资源列表",notes = "获取资源列表",httpMethod = "POST")
    @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header")
    public ResponseDto list(@RequestBody @Validated ResourceQuery resourceQuery) throws Exception {
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
       List<Resource> list=resourceService.getList(objectToMap(resourceQuery));
        rs.setData(list);
        return rs;
    }

    @PostMapping("auth_resource_list")
    @ApiOperation(value = "获取部门资源列表",notes = "获取部门资源列表",httpMethod = "POST")
    @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header")
    public ResponseDto deptlist(@RequestBody @Validated ResourceQuery resourceQuery) throws Exception {
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        rs.setData(resourceService.findDeptResource());
        return rs;
    }


    @PostMapping
    @ApiOperation(value = "新增或修改资源",notes = "新增或修改资源",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "resource", value = "资源", required = true, dataType = "Resource", paramType = "body")
    })
    public ResponseDto<Resource> save(@RequestBody @Validated Resource resource){
        ResponseDto rs=new ResponseDto<Role>(BizExceptionEnum.SUCCESSFUL_OPERATION);
        String[] pids=resource.getPids().split(",");
        String strs="";
        for(int i=0;i<pids.length;i++){
            strs+="["+pids[i]+"]";
            if(i!=pids.length-1){
                strs+=",";
            }
        }
        resource.setPids(strs);
        rs.setData(resourceService.saveOrUpdate(resource));
        return rs;
    }
    @PostMapping("del")
    @ApiOperation(value = "删除资源",notes = "删除资源",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "id", value = "资源", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseDto<Resource> del(@NotNull(message = "资源id不能为空") Long id){
        ResponseDto rs=new ResponseDto<Role>(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Resource resource=new Resource();
        resource.setId(id);
        resource.setStatus(-1);
        resourceService.update(resource);
        return rs;
    }



}
